#include <ddm/CtrlHandler.h>

#include <iostream>

namespace ddm {
CtrlHandler::CtrlHandler(
    openr::messaging::ReplicateQueue<openr::DecisionRouteUpdate>&
        routeUpdatesQueue,
    openr::OpenrEventBase* evb, std::string nodeId)
    : routeUpdatesQueue_(routeUpdatesQueue), evb_(evb) {
  // kvStoreClient_ =
  //     std::make_unique<openr::KvStoreClientInternal>(evb, nodeId, kvStore_);
}

folly::SemiFuture<folly::Unit> CtrlHandler::semifuture_insertRoute(
    std::unique_ptr<openr::thrift::UnicastRoute> route) {
  std::cout << "recv route" << std::endl;
  openr::DecisionRouteUpdate routeUpdate;

  auto nhs = std::unordered_set<openr::thrift::NextHopThrift>(
      route->nextHops()->begin(), route->nextHops()->end());

  auto entry = openr::RibUnicastEntry(openr::toIPNetwork(*route->dest()),
                                      std::move(nhs));

  routeUpdate.addRouteToUpdate(entry);

  routeUpdatesQueue_.push(routeUpdate);

  return folly::Unit();
}

folly::SemiFuture<folly::Unit> CtrlHandler::semifuture_submitRequirement(
    std::unique_ptr<ddm::thrift::Requirement> req) {
  std::cout << "recv submitted req" << std::endl;
  // apache::thrift::CompactSerializer serializer;
  // auto data = openr::writeThriftObjStr(*req, serializer);
  // kvStoreClient_->setKey(openr::AreaId{"0"}, "req:" + *req->name(), data);
  evb_->runInEventBaseThread([this, &req]() {
    apache::thrift::CompactSerializer serializer;
    // auto data = openr::writeThriftObjStr(*req, serializer);
    // std::cout << data << std::endl;
    // kvStoreClient_->setKey(openr::AreaId{"0"}, "req:" + *req->name(), "777");
  });

  return folly::Unit();
}

}  // namespace ddm